<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info"
                  show-icon/>

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('agent.list.username') + '/' + t('agent.list.nickname') })"
        />


        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table/>

        <!-- 表单 -->
        <PopupForm/>
        <Detail :isShow="isShow" @close="close()" :row="CurrRow"/>
    </div>
</template>

<script setup lang="ts">
import {provide, ref} from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import {defaultOptButtons} from '/@/components/table'
import {baTableApi} from '/@/api/common'
import {useAdminInfo} from '/@/stores/adminInfo'
import {useI18n} from 'vue-i18n'
import {TableColumnCtx} from "element-plus";
import Detail from "/@/views/backend/agent/list/detail.vue";
import {auth} from "/@/utils/common";

defineOptions({
    name: 'agent/list',
})

const {t} = useI18n()
const adminInfo = useAdminInfo()
let optBtn = defaultOptButtons(['edit', 'delete'])


const isShow = ref(false)
const CurrRow = ref()
const close = () => {
    isShow.value = false
}
// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        render: 'tipButton',
        // 按钮名称
        name: 'info',
        // 鼠标放置时的 title 提示
        title: '详情',
        // 直接在按钮内显示的文字，title 有值时可为空
        text: '详情',
        // 按钮类型，请参考 element plus 的按钮类型
        type: 'primary',
        // 按钮 icon
        icon: 'fa fa-search-plus',
        class: 'table-row-info',
        // tipButton 禁用 tip
        disabledTip: false,
        // 自定义点击事件
        click: (row: TableRow, field: TableColumn) => {
            isShow.value = true
            CurrRow.value = row
        },
        // 按钮是否显示，请返回布尔值
        display: (row: TableRow, field: TableColumn) => {
            // return auth('detail')
            return true
        },
    },
]

// 新按钮合入到默认的按钮数组
optBtn = newButton.concat(optBtn)


const baTable = new baTableClass(
    new baTableApi('/admin/agent.Agent/'),
    {
        column: [
            {type: 'selection', align: 'center', operator: false},
            {label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70},
            {
                label: t('agent.list.username'),
                prop: 'username',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query')
            },
            {
                label: t('agent.list.nickname'),
                prop: 'nickname',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query')
            },
            {label: "代理地区", prop: 'area.name', align: 'center'},
            {
                label: "平台抽成", prop: 'commission', align: 'center', render: 'customTemplate',
                customTemplate: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
                    return '<b style="color: red">' + value + '</b>%';
                }
            }, {
                label: "余额",
                prop: 'money',
                align: 'center',
                render: 'customTemplate',
                customTemplate: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
                    return '<b style="color: red">' + value + '</b>元';
                }
            },
            {
                label: t('agent.list.mobile'),
                prop: 'mobile',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query')
            },
            {
                label: t('agent.list.Last login'),
                prop: 'last_login_time',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160,
            },
            {
                label: t('Create time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160
            },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: {'0': 'danger', '1': 'success'},
                replaceValue: {'0': t('Disable'), '1': t('Enable')},
            },
            {
                label: t('Operate'),
                align: 'center',
                width: '200',
                render: 'buttons',
                buttons: optBtn,
                operator: false,
            },
        ],
        dblClickNotEditColumn: ['all'],
        showComSearch: true,
    },
    {
        defaultItems: {
            status: '1',
        },
    }
)

provide('baTable', baTable)

baTable.mount()
baTable.getIndex()
</script>

<style scoped lang="scss"></style>
